﻿@using System.Text.RegularExpressions
@using Discussion.Core.Mvc
@using Discussion.Web.Services.UserManagement.Avatar
@inject Discussion.Core.Models.SiteSettings SiteSettings
@inject IAvatarUrlService AvatarUrlService
@model TopicViewModel

@{
    ViewData["Title"] = Model.Topic.Title;
    
    ViewData["Keyword"] = Html.DescribeTopicType(Model.Topic.Type) + "：" + Model.Topic.Title;

    var descText = Model.Topic.Content.Length > 100 ? Model.Topic.Content.Substring(0, 100) + "..." : Model.Topic.Content;
    ViewData["Description"] = Regex.Replace(descText, @"(\n\r\t""'\>\<)", string.Empty);
}

<div class="row">
    <article class="panel panel-topic-detail col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <header class="topic-meta">
            <img src="@AvatarUrlService.GetAvatarUrl(Model.Topic.CreatedByUser)" class="topic-author-avatar"/>
            <h1> @Model.Topic.Title </h1>
            <div>
                <span class="meta-item author">
                    作者 @Model.Topic.CreatedByUser.DisplayName
                </span>
                <span class="meta-item created-at">
                    发布于 <span class="created-at-time" data-created-at="@Html.Timestamp(Model.Topic.CreatedAtUtc)"></span>
                </span>
                <span class="meta-item view-count">
                    @Model.Topic.ViewCount 次浏览
                </span>
                <span class="meta-item type">
                    @Html.DescribeTopicType(Model.Topic.Type)
                </span>
            </div>
        </header>
        <article class="topic-content">
            @Html.RenderMarkdown(Model.Topic.Content)
        </article>
    </article>
</div>

@if (Model.Topic.ReplyCount > 0)
{
    <partial name="./Partial/_ReplyList.cshtml" for="@Model" />
}

@if (Context.IsAuthenticated() && SiteSettings.CanAddNewReplies())
{
    <partial name="./Partial/_NewReply.cshtml" for="@Model" />
}

@section Tail{
    <script>
        $(function() {
            var replyUrl = '@Url.Action("Reply", "Reply", new {topicId = Model.Id})';
            var previewApi = '@Url.Action("RenderMarkdown", "Common", new {maxHeadingLevel = 3})';
            var app = window.DiscussionApp;
            var previewSelectors = {
                tabPreview: '#tab-preview',
                tabEditor: '#tab-editor',
                contentPreview: '#preview',
                contentEditor: '#content-editor'
            };
            
            app.utils.transformTimestampOn('.created-at-time', 'data-created-at');
            app.utils.transformTimestampOn('.replied-at', 'data-replied-at');

            $("#submit-create").click(app.topic.submitNewReply("#content-editor", replyUrl));
            app.topic.setupReplyPreview(previewSelectors, previewApi);
        });
    </script>
}